package com.xy.smartsms.db.carrierparam;

import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;

import com.xy.smartsms.db.carrierparam.entity.RecognitionErrorItem;
import com.xy.smartsms.util.Log;
import com.xy.smartsms.util.ThreadManager;

import org.json.JSONObject;

import java.util.UUID;

import cn.com.xy.sms.sdk.dex.DexUtil;
import cn.com.xy.sms.sdk.net.NetUtil;
import cn.com.xy.sms.sdk.util.ConversationManager;

/**
 * Created by kangwenbo on 2017/3/9.
 */

public class RecognitionErrManager {

    private static RecognitionErrManager mInstance;
    private final String TAG = "RecognitionErrManager";
    private TelephonyManager mTelephonyManager;

    private RecognitionErrManager(Context context) {
        mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    }
    public static synchronized RecognitionErrManager getInstance(Context context) {
        synchronized (RecognitionErrManager.class) {
            if(mInstance == null){
                mInstance = new RecognitionErrManager(context);
            }
        }
        return mInstance;
    }

    public void insertMenuErr(final RecognitionErrorItem cmccItem, final RecognitionErrorItem xyItem) {
        ThreadManager.NUMBER_UPDATE_CARRIER.execute(new Runnable() {
            @Override
            public void run() {
                String uuid = UUID.randomUUID().toString();
                JSONObject menuErr1 = buildMenuData(getPublicInfo(), cmccItem);
                JSONObject menuErr2 = buildMenuData(getPublicInfo(), xyItem);
                ConversationManager.saveLogIn(uuid, "cn.com.xy.sms.sdk.db.carrierparam.RecogErrManager", "insertMenuErr",
                        menuErr1);
                ConversationManager.saveLogIn(uuid, "cn.com.xy.sms.sdk.db.carrierparam.RecogErrManager", "insertMenuErr",
                        menuErr2);
            }
        });
    }

    public void insertSceneErr(final RecognitionErrorItem cmccItem, final RecognitionErrorItem xyItem) {
        ThreadManager.NUMBER_UPDATE_CARRIER.execute(new Runnable() {
            @Override
            public void run() {
                String uuid = UUID.randomUUID().toString();
                JSONObject sceneErr1 = buildSceneData(getPublicInfo(), cmccItem);
                JSONObject sceneErr2 = buildSceneData(getPublicInfo(), xyItem);
                ConversationManager.saveLogIn(uuid, "cn.com.xy.sms.sdk.db.carrierparam.RecogErrManager", "insertSceneErr",
                        sceneErr1);
                ConversationManager.saveLogIn(uuid, "cn.com.xy.sms.sdk.db.carrierparam.RecogErrManager", "insertSceneErr",
                        sceneErr2);
            }
        });

    }

    private JSONObject getPublicInfo() {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("IMEI", mTelephonyManager.getDeviceId());
            jsonObject.put("phoneModel", Build.BRAND + " " + Build.MODEL);
            jsonObject.put("androidVer", Build.VERSION.RELEASE);
            jsonObject.put("phoneOsVer", Build.VERSION.INCREMENTAL);
            jsonObject.put("SDK_CMCC_Ver", 1);
            jsonObject.put("SDK_CMCC_ALGORITHM", 1);
            jsonObject.put("SDK_XY_Ver", NetUtil.APPVERSION);
            jsonObject.put("SDK_XY_ALGORITHM", DexUtil.getSuanfaVersion());
            jsonObject.put("SDK_XY_UI", DexUtil.getUIVersion());
        } catch (Throwable t) {
            Log.e(TAG, "getPublicInfo", t);
        }
        return jsonObject;
    }

    public JSONObject buildMenuData(JSONObject jsonObject, RecognitionErrorItem item) {
        try {
            jsonObject.put("sdkType", item.getSdkType());
            jsonObject.put("isRecognize", item.isRecognise() ? 1 : 0);
            jsonObject.put("company", item.getCompany() == null ? "" : item.getCompany());
            jsonObject.put("errorType", item.getErrorType() == null ? "" : item.getErrorType());
        } catch (Throwable t) {
            Log.e(TAG, "buildMenuData", t);
        }
        return jsonObject;
    }

    public JSONObject buildSceneData(JSONObject jsonObject, RecognitionErrorItem item) {
        try {
            jsonObject.put("sdkType", item.getSdkType());
            jsonObject.put("isRecognize", item.isRecognise() ? 1 : 0);
            jsonObject.put("scene", item.getScene() == null ? "null" : item.getScene());
            jsonObject.put("recogTime", item.getRecogniseTime() == null ? "0" : item.getRecogniseTime());
            jsonObject.put("recogTimeType", item.getRecognizeTimeType());
        } catch (Throwable t) {
            Log.e(TAG, "buildSceneData", t);
        }
        return jsonObject;
    }

}
